package com.mogujie.tt.ui.fragment;



import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.DB.sp.RegisterSp;
import com.mogujie.tt.R;
import com.mogujie.tt.imservice.event.RegisterEvent;
import com.mogujie.tt.ui.activity.MyUserInfoActivity;
import com.mogujie.tt.ui.activity.SxunActivity;
import com.mogujie.tt.ui.activity.TotalSettingActivity;
import com.mogujie.tt.ui.widget.IMBaseImageView;

import de.greenrobot.event.EventBus;


public class FragmentProfile extends MainFragment {


    private View curView = null;
    private String avatar = "";
    private IMBaseImageView iv_avatar;
    private TextView tv_name;
    TextView tv_phone;
    String phone;
    String nick;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);//注册事件
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        if (null != curView) {
            ((ViewGroup) curView.getParent()).removeView(curView);
            return curView;
        }
        curView = inflater.inflate(R.layout.fragment_profile, topContentView);

        return curView;
    }

    @Override
    protected void initHandler() {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 设置顶部标题栏
       // setTopTitle(getActivity().getString(R.string.page_me));
        setTopTitleBold("我");
        setTopRightButton(R.drawable.ty_btn_jty);

        RelativeLayout re_myinfo = (RelativeLayout) getView().findViewById(R.id.re_myinfo);//个人信息

        re_myinfo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyUserInfoActivity.class));
            }

        });

        RelativeLayout re_setting = (RelativeLayout) getView().findViewById(R.id.re_setting);//设置
        re_setting.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), TotalSettingActivity.class));
            }

        });

        RelativeLayout re_shoucang =(RelativeLayout)getView().findViewById(R.id.re_shoucang);//时迅云
        re_shoucang.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SxunActivity.class));
            }
        });

        nick = RegisterSp.instance().getSpDetailedInfo().getDetailedNickName();
        phone = RegisterSp.instance().getSpDetailedInfo().getDetailedMobilePhone();
        avatar = RegisterSp.instance().getSpDetailedInfo().getDetailedHeadPortrait();//头像url地址

        iv_avatar = (IMBaseImageView) re_myinfo.findViewById(R.id.iv_avatar);
        tv_name = (TextView) re_myinfo.findViewById(R.id.tv_name);
        tv_phone = (TextView) re_myinfo.findViewById(R.id.tv_fxid);

        tv_name.setText(nick);
        tv_phone.setText(phone);
        showUserAvatar();
        re_myinfo.findViewById(R.id.qr_code).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"二维码",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void showUserAvatar() {
        iv_avatar.setDefaultImageRes(R.drawable.tt_default_user_portrait_corner);
        iv_avatar.setCorner(15);
        iv_avatar.setImageResource(R.drawable.tt_default_user_portrait_corner);
        iv_avatar.setImageUrl(RegisterSp.instance().getSpDetailedInfo().getDetailedHeadPortrait());
    }


    @Override
    public void onResume() {
        super.onResume();
        synchronousUpdateMyPage();
    }


    private void synchronousUpdateMyPage(){

        String vatar_temp = RegisterSp.instance().getSpDetailedInfo().getDetailedHeadPortrait();
        if (vatar_temp != null&&!vatar_temp.equals(avatar)) {
            showUserAvatar();}

        String nick_temp = RegisterSp.instance().getSpDetailedInfo().getDetailedNickName();
        if (nick_temp != null&&!nick_temp.equals(nick)) {tv_name.setText(nick_temp);}

        String phone_temp = RegisterSp.instance().getSpDetailedInfo().getDetailedMobilePhone();
        if (phone_temp != null&&!phone_temp.equals(phone)) {tv_phone.setText(phone_temp);}
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//卸载注册事件
    }

    /**
     * ----------------------------event 事件驱动----------------------------
     */
    public void onEventMainThread(RegisterEvent.MyPage event) {
        switch (event) {
            case UPDATE_MY_PAGE:
                synchronousUpdateMyPage();
                break;
        }
    }
}
